package cn.kzgg.system.service.impl;

import cn.kzgg.basic.service.impl.BaseServiceImpl;
import cn.kzgg.system.domain.Role;
import cn.kzgg.system.mapper.RoleMapper;
import cn.kzgg.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KZGG
 * @since 2022-06-21
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 既要添加本身信息，又要保存中间表
     */
    @Override
    public void save(Role role) {
        roleMapper.save(role);

        //保存中间表
        initRelationTable(role);
    }

    private void initRelationTable(Role role) {
        //一、删除历史数据
        roleMapper.removeRoleMenuByRoleId(role.getId());
        roleMapper.removeRolePermissionByRoleId(role.getId());
        //二、保存新的数据
        roleMapper.saveRoleMenu(role);
        roleMapper.saveRolePermission(role);

    }

    //后面自己做其他功能的关联
    @Override
    public void update(Role role) {
        super.update(role);
    }

    @Override
    public void removeById(Long id) {
        super.removeById(id);
    }
}
